<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="客户编码 外键customer表:">
        <el-input v-model="queryParams.customerId" size="small" placeholder="请输入客户编码 外键customer表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="费用编码，仓储费，包装费 ，叉车费。需要一个表维护，我们的tms项目中有，可以搬过来该表:">
        <el-input v-model="queryParams.frtNo" size="small" placeholder="请输入费用编码，仓储费，包装费 ，叉车费。需要一个表维护，我们的tms项目中有，可以搬过来该表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="费用名称:">
        <el-input v-model="queryParams.frtName" size="small" placeholder="请输入费用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="结算基于 pack包装qty数量、vol 面积，weight重量，固定一个价格，就是同意价。:">
        <el-input v-model="queryParams.settleBy" size="small" placeholder="请输入结算基于 pack包装qty数量、vol 面积，weight重量，固定一个价格，就是同意价。" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="结算时间，按天按月:">
        <el-date-picker v-model="queryParams.chargeTimeBy" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择结算时间，按天按月" clearable />
      </el-form-item>
      <el-form-item label="金额，也可以百分比:">
        <el-input v-model="queryParams.amount" size="small" placeholder="请输入金额，也可以百分比" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入库费还是出库费:">
        <el-input v-model="queryParams.inAndOutCharge" size="small" placeholder="请输入入库费还是出库费" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="有效期到:">
        <el-input v-model="queryParams.validUntil" size="small" placeholder="请输入有效期到" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="转换单位:">
        <el-input v-model="queryParams.convertQuantity" size="small" placeholder="请输入转换单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始数量:">
        <el-input v-model="queryParams.startQuantity" size="small" placeholder="请输入开始数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大数量:">
        <el-input v-model="queryParams.endQuantity" size="small" placeholder="请输入最大数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="有效期开始日:">
        <el-date-picker v-model="queryParams.startDateFrom" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择有效期开始日" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:chargeConfig:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:chargeConfig:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:chargeConfig:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:chargeConfig:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编码 外键customer表" align="center" prop="customerId" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="customerName" />
      <el-table-column label="费用编码，仓储费，包装费 ，叉车费。需要一个表维护，我们的tms项目中有，可以搬过来该表" align="center" prop="frtNo" />
      <el-table-column label="费用名称" align="center" prop="frtName" />
      <el-table-column label="结算基于 pack包装qty数量、vol 面积，weight重量，固定一个价格，就是同意价。" align="center" prop="settleBy" />
      <el-table-column label="结算时间，按天按月" align="center" prop="chargeTimeBy" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeTimeBy, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额，也可以百分比" align="center" prop="amount" />
      <el-table-column label="金额还是百分比 amt/ %" align="center" prop="chargeType" />
      <el-table-column label="客户类型" align="center" prop="customerType" />
      <el-table-column label="入库费还是出库费" align="center" prop="inAndOutCharge" />
      <el-table-column label="有效期到" align="center" prop="validUntil" />
      <el-table-column label="转换单位" align="center" prop="convertQuantity" />
      <el-table-column label="开始数量" align="center" prop="startQuantity" />
      <el-table-column label="最大数量" align="center" prop="endQuantity" />
      <el-table-column label="有效期开始日" align="center" prop="startDateFrom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDateFrom, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['pms:chargeConfig:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['pms:chargeConfig:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="chargeConfigFormDialog.title" :visible.sync="chargeConfigFormDialog.visible" :close-on-click-modal="false" :before-close="resetChargeConfigFormDialog" width="500px" append-to-body>
      <el-form ref="chargeConfigForm" :model="chargeConfigFormData" :rules="chargeConfigFormRules" label-width="80px">
        <el-form-item label="客户编码 外键customer表" prop="customerId">
          <el-input v-model="chargeConfigFormData.customerId" placeholder="请输入客户编码 外键customer表" />
        </el-form-item>
        <el-form-item label="" prop="customerName">
          <el-input v-model="chargeConfigFormData.customerName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="费用编码，仓储费，包装费 ，叉车费。需要一个表维护，我们的tms项目中有，可以搬过来该表" prop="frtNo">
          <el-input v-model="chargeConfigFormData.frtNo" placeholder="请输入费用编码，仓储费，包装费 ，叉车费。需要一个表维护，我们的tms项目中有，可以搬过来该表" />
        </el-form-item>
        <el-form-item label="费用名称" prop="frtName">
          <el-input v-model="chargeConfigFormData.frtName" placeholder="请输入费用名称" />
        </el-form-item>
        <el-form-item label="结算基于 pack包装qty数量、vol 面积，weight重量，固定一个价格，就是同意价。" prop="settleBy">
          <el-input v-model="chargeConfigFormData.settleBy" placeholder="请输入结算基于 pack包装qty数量、vol 面积，weight重量，固定一个价格，就是同意价。" />
        </el-form-item>
        <el-form-item label="结算时间，按天按月" prop="chargeTimeBy">
          <el-date-picker v-model="chargeConfigFormData.chargeTimeBy" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择结算时间，按天按月" clearable />
        </el-form-item>
        <el-form-item label="金额，也可以百分比" prop="amount">
          <el-input v-model="chargeConfigFormData.amount" placeholder="请输入金额，也可以百分比" />
        </el-form-item>
        <el-form-item label="入库费还是出库费" prop="inAndOutCharge">
          <el-input v-model="chargeConfigFormData.inAndOutCharge" placeholder="请输入入库费还是出库费" />
        </el-form-item>
        <el-form-item label="有效期到" prop="validUntil">
          <el-input v-model="chargeConfigFormData.validUntil" placeholder="请输入有效期到" />
        </el-form-item>
        <el-form-item label="转换单位" prop="convertQuantity">
          <el-input v-model="chargeConfigFormData.convertQuantity" placeholder="请输入转换单位" />
        </el-form-item>
        <el-form-item label="开始数量" prop="startQuantity">
          <el-input v-model="chargeConfigFormData.startQuantity" placeholder="请输入开始数量" />
        </el-form-item>
        <el-form-item label="最大数量" prop="endQuantity">
          <el-input v-model="chargeConfigFormData.endQuantity" placeholder="请输入最大数量" />
        </el-form-item>
        <el-form-item label="有效期开始日" prop="startDateFrom">
          <el-date-picker v-model="chargeConfigFormData.startDateFrom" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择有效期开始日" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmChargeConfigFormDialog">确 定</el-button>
        <el-button @click="resetChargeConfigFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ChargeConfigApi from '@/api/pms/ChargeConfigApi'

const defaultChargeConfigFormData = {
  customerId: null,
  customerName: null,
  frtNo: null,
  frtName: null,
  settleBy: null,
  chargeTimeBy: null,
  amount: null,
  chargeType: null,
  customerType: null,
  inAndOutCharge: null,
  validUntil: null,
  convertQuantity: null,
  startQuantity: null,
  endQuantity: null,
  startDateFrom: null
}
export default {
  name: 'ChargeConfigList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerId: null,
        customerName: null,
        frtNo: null,
        frtName: null,
        settleBy: null,
        chargeTimeBy: null,
        amount: null,
        chargeType: null,
        customerType: null,
        inAndOutCharge: null,
        validUntil: null,
        convertQuantity: null,
        startQuantity: null,
        endQuantity: null,
        startDateFrom: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      chargeConfigFormDialog: {
        title: '',
        visible: false
      },
      chargeConfigFormData: Object.assign({}, defaultChargeConfigFormData),
      chargeConfigFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openChargeConfigFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openChargeConfigFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      ChargeConfigApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openChargeConfigFormDialog(id) {
      this.chargeConfigFormDialog.visible = true
      if (id) {
        this.chargeConfigFormDialog.title = '仓储费率配置-修改'
        ChargeConfigApi.get(id).then(response => {
          this.chargeConfigFormData = {
            ...this.chargeConfigFormData,
            ...response.data
          }
        })
      } else {
        this.chargeConfigFormDialog.title = '仓储费率配置-添加'
      }
    },
    confirmChargeConfigFormDialog() {
      this.$refs['chargeConfigForm'].validate(valid => {
        if (valid) {
          if (this.chargeConfigFormData.id) {
            ChargeConfigApi.edit(this.chargeConfigFormData).then(response => {
              this.$message.success('修改成功')
              this.resetChargeConfigFormDialog()
              this.handleQuery()
            })
          } else {
            ChargeConfigApi.add(this.chargeConfigFormData).then(response => {
              this.$message.success('添加成功')
              this.resetChargeConfigFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetChargeConfigFormDialog() {
      this.chargeConfigFormDialog = {
        title: '',
        visible: false
      }
      this.chargeConfigFormData = Object.assign({}, defaultChargeConfigFormData)
      this.resetForm('chargeConfigForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
